package com.vueapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vueapi.domain.HjComment;
import com.vueapi.domain.HjExceptionLog;
import com.vueapi.domain.VisitLog;
import com.vueapi.service.VisitLogService;
import com.vueapi.mapper.VisitLogMapper;
import com.vueapi.utils.DateUtils;
import com.vueapi.utils.ParseUtils;
import com.vueapi.utils.ResultUtils;
import com.vueapi.utils.TimeAgoUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【hj_visit_log】的数据库操作Service实现
* @createDate 2023-08-05 18:01:55
*/
@Service
public class VisitLogServiceImpl extends ServiceImpl<VisitLogMapper, VisitLog>
    implements VisitLogService{

    @Override
    public List<Integer> getVisitAddCount() {
        ArrayList<Integer> dataCountVOS = new ArrayList<>();
        for (int i = 7; i >= 1; i--) {

            Date today = TimeAgoUtils.getTodayZeroTimePre(i);
            Date lastDay = null;
            // 0代表了今天，那么就要算今天到明天，所以要调用往后加天数的方法 暂时取消
            lastDay = TimeAgoUtils.getTodayZeroTimePre(i-1);

            LambdaQueryWrapper<VisitLog> between = new LambdaQueryWrapper<VisitLog>().between(VisitLog::getCreateTime, today,lastDay);
            dataCountVOS.add((int) count(between));
        }
        return dataCountVOS;
    }

    @Override
    public ResultUtils listPage(Integer pageNum, Integer pageSize, String ip, String ipSource) {
        LambdaQueryWrapper<VisitLog> cw = new LambdaQueryWrapper<>();
        cw.like(!ip.equals(""),VisitLog::getIp,ip);
        cw.like(!ipSource.equals(""),VisitLog::getIpSource,ipSource);
        cw.orderByDesc(VisitLog::getCreateTime);

        Page<VisitLog> operationLogPage = this.page(new Page<>(pageNum, pageSize), cw);

        return ResultUtils.success(operationLogPage);
    }

    @Override
    public Boolean visitDeleteByIds(String ids) {
        List<Integer> operationIds = ParseUtils.intArrayToList(ParseUtils.stringToIntArray(ids));
        return removeByIds(operationIds);
    }
}




